package com.zhxy.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.LeaveDao;
import com.zhxy.manager.dto.LeaveDto;
import com.zhxy.manager.entity.Course;
import com.zhxy.manager.entity.Leave;
import com.zhxy.manager.service.intf.LeaveService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ZM
 */
@Service
public class LeaveServiceImpl implements LeaveService {
    @Autowired
    private LeaveDao dao;

    @Override
    public R save(Leave leave) {
        if (dao.add(leave) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public PageBean queryDetail(String name, int page, int limit) {
        //验证查询条以  如果为 " "  那么赋值null
        if (!StringUtils.hasLength(name)) {
            name = null;
        } else {
            name = "%"+ name +"%"; //拼接上模糊查询
        }
        //分页
        PageHelper.startPage(page, limit);
        //获取分页结果结果
        PageInfo<LeaveDto> pageInfo = new PageInfo<>(dao.selectDetail(name));
        //返回分页数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    //根据id删除
    @Override
    public R del(int id) {
        if (id > 0) {
            if (dao.del(id) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        } else {
            return R.fail();
        }
    }

    //修改
    @Override
    public R update(Leave leave) {
        if (dao.update(leave) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    //批量删除
    @Override
    public R delBatch(int[] ids) {
        if (ids != null && ids.length > 0) {
            //执行
            if (dao.delBatch(ids) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public List<Leave> queryAll() {
        return dao.selectAll();
    }
}
